package controller;


import DB.DBConnection;
import com.sun.javaws.progress.PreloaderPostEventListener;
import entity.Admin;
import entity.Reader;
import service.LoginService;
import service.impl.LoginServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;


@WebServlet("/Login")
public class LoginServlet extends HttpServlet {



    /**
     * 处理登录的业务逻辑
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws  IOException {
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        //从页面获取提交的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String type = request.getParameter("type");
        //调用ServiceLogin中的方法，把获取的值传递给service
        LoginService loginService=new LoginServiceImpl();
        try {
            int k=loginService.login(username,password);
            if (k>0)
            {
                request.getRequestDispatcher("index.jsp").forward(request,response);
            }else
            {
                request.getRequestDispatcher("login.jsp").forward(request,response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }


    }

}
